package system

import (
	"context"
	"fmt"
	"github.com/samber/lo"
	"private-domain-overseas-service/admin/business/api/internal/repository/system"
	"private-domain-overseas-service/pkg/status/bizsystemsmsrecord"
	"private-domain-overseas-service/pkg/xerr"
	"private-domain-overseas-service/tools/ent"
	"private-domain-overseas-service/tools/ent/hzsystemsmsrecord"
	"private-domain-overseas-service/tools/ent/predicate"
	"time"

	"private-domain-overseas-service/admin/business/api/internal/svc"
	"private-domain-overseas-service/admin/business/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SystemSmsRecordListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// NewSystemSmsRecordListLogic - 手机号验证码列表
func NewSystemSmsRecordListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemSmsRecordListLogic {
	return &SystemSmsRecordListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SystemSmsRecordListLogic) SystemSmsRecordList(req *types.SystemSmsRecordListRequest) (resp *types.SystemSmsRecordListResponse, err error) {
	// 创建实例
	smsDao := system.NewSystemSmsRecordDao(l.ctx, l.svcCtx)

	// 分页查询短信列表
	systemSmsConditions := make([]predicate.HzSystemSmsRecord, 0)
	systemSmsConditions = append(systemSmsConditions, hzsystemsmsrecord.DeleteAtIsNil())
	if len(req.SystemSmsPhone) > 0 {
		systemSmsConditions = append(systemSmsConditions, hzsystemsmsrecord.SystemSmsPhoneEQ(req.SystemSmsPhone))
	}
	if len(req.SystemSmsArea) > 0 {
		systemSmsConditions = append(systemSmsConditions, hzsystemsmsrecord.SystemSmsAreaEQ(req.SystemSmsArea))
	}
	verifyList, total, err := smsDao.SystemSmsRecordConditionsAllByPage(systemSmsConditions, req.Current, req.PageSize)
	if err != nil {
		return nil, xerr.ErrCodeMsg(xerr.ServerOperationExceptionError, fmt.Sprintf("分页查询短信列表失败: %v", err.Error()))
	}

	// 组装返回数据
	list := lo.Map(verifyList, func(item *ent.HzSystemSmsRecord, _ int) *types.SystemSmsRecordInfo {
		var validStatus int64
		if time.Unix(item.SystemSmsExpireAt, 0).Before(time.Now()) || item.SystemSmsStatus == bizsystemsmsrecord.SmsStatusUse {
			validStatus = bizsystemsmsrecord.Invalid
		} else {
			validStatus = bizsystemsmsrecord.Effective
		}
		return &types.SystemSmsRecordInfo{
			Id:                   int64(item.ID),
			SystemSmsArea:        item.SystemSmsArea,
			SystemSmsPhone:       item.SystemSmsPhone,
			SystemSmsCaptcha:     item.SystemSmsCaptcha,
			SystemSmsMessage:     item.SystemSmsMessage,
			SystemSmsDescription: item.SystemSmsDescription,
			SystemSmsType:        item.SystemSmsType,
			SystemSmsPlatform:    item.SystemSmsPlatform,
			SystemSmsStatus:      item.SystemSmsStatus,
			SystemSmsValidStatus: validStatus,
			SystemSmsSendAt:      time.Unix(item.SystemSmsSendAt, 0).Format(time.DateTime),
			SystemSmsExpireAt:    time.Unix(item.SystemSmsExpireAt, 0).Format(time.DateTime),
			CreateAt:             time.Unix(item.CreatedAt, 0).Format(time.DateTime),
			UpdateAt:             time.Unix(item.UpdateAt, 0).Format(time.DateTime),
		}
	})

	// 返回数据
	resp = &types.SystemSmsRecordListResponse{
		Page: &types.PageResponse{
			Total:           total,
			Current:         req.Current,
			PageSize:        req.PageSize,
			RequestPageSize: req.PageSize,
		},
		List: list,
	}

	return
}
